# -*- coding: utf-8 -*-
import logging
import os
class loghelp:
    def __init__(self,loggername,loggerdir,servername,errname):
        self.loggername=loggername;
        self.loggerdir=loggerdir;
        self.loggerpath=os.path.join(self.loggerdir,servername);
        self.errorpath=os.path.join(self.loggerdir,errname);
        if(not os.path.exists(self.loggerdir)):
            os.makedirs(self.loggerdir)
    def getLogger(self):
        #/logs/xkw_server.log
        logger = logging.getLogger(self.loggername)
        logger.setLevel(logging.DEBUG)
        # 建立一个filehandler来把日志记录在文件里，级别为debug以上
        fh = logging.FileHandler(self.loggerpath)
        fh.setLevel(logging.DEBUG)
        eh = logging.FileHandler(self.errorpath)
        eh.setLevel(logging.ERROR)
        # 建立一个streamhandler来把日志打在CMD窗口上，级别为error以上
        ch = logging.StreamHandler()
        ch.setLevel(logging.ERROR)
        # 设置日志格式
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        ch.setFormatter(formatter)
        fh.setFormatter(formatter)
        eh.setFormatter(formatter)
        # 将相应的handler添加在logger对象中
        logger.addHandler(ch)
        logger.addHandler(fh)
        logger.addHandler(eh)
        return logger
